package Controller.Music;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import utility.Paging;
import Bean.JoinBean;
import Bean.MusicBean;
import Bean.rowBean;
import Dao.MusicDao;

public class MusicListController implements Controller {
	private MusicDao dao;
	Paging pageInfo = null;
	public void setDao(MusicDao dao) {
		this.dao = dao;
	}

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse arg1) throws Exception {
		
		String pageNumber = request.getParameter("pageNumber");
		String pageSize = request.getParameter("pageSize");
		String id = request.getParameter("id");
		String page = request.getParameter("page");
		
		String url = "";
		String contextPath = request.getContextPath();
		System.out.println(contextPath);
		url = contextPath + "/MusicList.music";
		
		//------------최신 1달간 발매된 노래 찾기------------------
		Calendar cal = java.util.Calendar.getInstance();
		//현재 년도, 월, 일
		String year = Integer.toString(cal.get( cal.YEAR ));
		String month = Integer.toString(cal.get( cal.MONTH ) + 1 );
		month = month.length() > 1 ? month : "0"+month;
		String day = Integer.toString(cal.get( cal.DAY_OF_MONTH));
		day = day.length() > 1 ? day : "0"+day;
		
		String nowYMD = year+month+day; //현재 yyyymmdd
		
		cal.add(cal.WEEK_OF_MONTH, -32); //4주전 시간
		
		year = Integer.toString(cal.get( cal.YEAR ));
		month = Integer.toString(cal.get( cal.MONTH ) + 1 );
		month = month.length() > 1 ? month : "0"+month;
		day = Integer.toString(cal.get( cal.DAY_OF_MONTH));
		day = day.length() > 1 ? day : "0"+day;
		
		String agoYMD = year+month+day;	//4주전 시간
		
		int intNowYMD = Integer.parseInt(nowYMD);
		int intAgoYMD = Integer.parseInt(agoYMD);
		rowBean row = new rowBean();
		
		row.setIntAgoYMD(intAgoYMD);
		row.setIntNowYMD(intNowYMD);
		
		int totalCount = 0;
		String title = null;
		if(page.equals("newmusic")){
			title = "최신음악";
			totalCount = dao.getNewTotal(row);
		} else if(page.equals("ballade")){
			title = "발라드";
			totalCount = dao.getBalladeTotal(row);
		} else if(page.equals("dance")){
			title = "댄스";
			totalCount = dao.getDanceTotal(row);
		} else if(page.equals("hiphop")){
			title = "힙합";
			totalCount = dao.getHiphopTotal(row);
		} else if(page.equals("rock")){
			title = "락";
			totalCount = dao.getRockTotal(row);
		} else if(page.equals("ost")){
			title = "OST";
			totalCount = dao.getOSTTotal(row);
		}
		
		String param ="?page="+page;
		pageInfo = new Paging(pageNumber, pageSize, totalCount, url, param);
		
		row.setBeginRow(pageInfo.getBeginRow());
		row.setEndRow(pageInfo.getEndRow());
		
		//System.out.println(pageInfo.getBeginRow());
		//System.out.println(pageInfo.getEndRow());
		
		List<JoinBean> newList = null;
		if(page.equals("newmusic")){
			newList = dao.GetNewMusicList(row);
		} else if(page.equals("ballade")){
			newList = dao.GetBalladeMusicList(row);
		} else if(page.equals("dance")){
			newList = dao.GetDanceMusicList(row);
		} else if(page.equals("hiphop")){
			newList = dao.GetHiphopMusicList(row);
		} else if(page.equals("rock")){
			newList = dao.GetRockMusicList(row);
		} else if(page.equals("ost")){
			newList = dao.GetOSTMusicList(row);
		}
		
		for (JoinBean joinBean : newList) {
			MusicBean musicBean = joinBean.getMusicBean();
			if(joinBean.getMusicBean().getMs_featuring().equals("없음")){
				musicBean.setMs_featuring("");
				joinBean.setMusicBean(musicBean);
			}else{
				musicBean.setMs_featuring("(feat."+musicBean.getMs_featuring()+")");
				joinBean.setMusicBean(musicBean);
			}
		}
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("musicList");
		mav.addObject("newList",newList);
		mav.addObject("title", title);
		mav.addObject("pageInfo", pageInfo);
		return mav;
	}
	

}
